﻿using System;
using System.Configuration;
using System.IO;
using Qiniu.Extend;
using Util.Exceptions;

namespace IterationLife.Extensions.Qiniu
{
    /// <summary>
    /// 七牛云
    /// </summary>
    public class QiNiu
    {
        /// <summary>
        /// AccessKey
        /// </summary>
        public static string AccessKey => ConfigurationManager.AppSettings["QiNiu_AccessKey"];
        /// <summary>
        /// SevretKey
        /// </summary>
        public static string SevretKey => ConfigurationManager.AppSettings["QiNiu_SevretKey"];
        /// <summary>
        /// 空间
        /// </summary>
        public static string Bucket => ConfigurationManager.AppSettings["QiNiu_Bucket"];
        /// <summary>
        /// 域名
        /// </summary>
        public static string DomainName => ConfigurationManager.AppSettings["QiNiu_DomainName"];

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            QiniuUploadClient.Intialize(Bucket, AccessKey, SevretKey);
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="fileStream">文件流</param>
        /// <param name="saveName">保存名</param>
        public static void Upload(Stream fileStream, string saveName)
        {
            try
            {
                var qiniu = new QiniuUploadClient();
                qiniu.Upload(fileStream, saveName);
            }
            catch (Exception e)
            {
                throw new Warning(e.Message);
            }
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="fileBytes">文件流</param>
        /// <param name="saveName">保存名</param>
        public static void Upload(byte[] fileBytes, string saveName)
        {
            try
            {
                var qiniu = new QiniuUploadClient();
                qiniu.Upload(fileBytes, saveName);
            }
            catch (Exception e)
            {
                throw new Warning(e.Message);
            }
        }

    }
}
